package com.tg.app.adapter;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.tange.module.camera.hub.CameraHub;
import com.tg.app.R;
import com.tg.app.helper.DeviceHelper;
import com.tange.base.toolkit.StringUtils;
import com.tg.appcommon.android.TGLog;
import com.tg.data.bean.DeviceItem;
import com.tg.app.camera.Camera;
import com.tg.app.camera.Cs2Camera;
import com.tange.base.toolkit.AppUtil;
import com.tg.app.widget.SplitScreenPlayer;

import java.util.List;

/**
 * Describe:
 * Created by Rance Rang on 2019-12-27.
 * <p>
 * Copyright (c) Tange.Inc All rights reserved.
 */
public class SinglePlayAdapter extends RecyclerView.Adapter<SinglePlayAdapter.SinglePlayViewHolder> implements SplitScreenPlayer.OnSplitScreenPlayerListener{
    private static final String TAG = "SinglePlayAdapter";
    private Context  context;
    private List<DeviceItem> itemList;
    private  int  mPageSize = 0;
    private int mIndex = 0;;
    private  int mPageCount = 0;;
    private CameraHub cameraHub;
    public SinglePlayAdapter(Context ctx,List<DeviceItem> items){
        context = ctx;
        itemList  = items;
        cameraHub = CameraHub.getInstance();
    }

    public void setIndex(int mIndex) {
        this.mIndex = mIndex;
    }
    public void setPageSize(int mPageSize) {
        this.mPageSize = mPageSize;
    }

    public void setPageCount(int mPageCount) {
        this.mPageCount = mPageCount;
    }

    @NonNull
    @Override
    public SinglePlayViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.layout_quarter_screen_item, parent, false);
        return new SinglePlayViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull SinglePlayViewHolder holder, int position) {
        if (holder != null) {

            int width= (context.getResources().getDisplayMetrics().widthPixels + AppUtil.getStatusBarHeight(context) )/ 2;
            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(width, ViewGroup.LayoutParams.MATCH_PARENT);
            holder.layout.setLayoutParams(layoutParams);
            // 计算item索引
//            int index = mIndex * mPageSize + position;
            // 设置索引值到tag
            holder.itemView.setTag(position);
            DeviceItem item = itemList.get(position);
            if (item!=null) {
                holder.tvName.setText(item.name);
                //todo p2p_id 为空 默认为tutk
                if (!TextUtils.isEmpty(item.p2p_id) || DeviceHelper.isExistLockBellRelay(item)) { // 尚云 p2p
                    Camera camera = cameraHub.getCamera(item.uuid);
                    if (camera == null){
                        if (!StringUtils.isEmpty(item.tg_relay)){
                            camera = CameraHub.getInstance().getCamera(DeviceItem.createBasicDevice(item));
                        }else {
                            String p2pId = item.p2p_id.substring(0, 18);// 截取18位才是正确的p2pid
                            camera = new Cs2Camera(item.uuid, p2pId, item.name, item.password);
                        }
                    }

                    camera.deviceId = item.id;
                    if (CameraHub.getInstance().isEnableLocalApConnect()) {
                        camera.enableLanSearch();
                    } else {
                        camera.isShared = (item.bind_type != DeviceItem.USER_BIND);
                        camera.desKey = item.des_key;
                    }

                    holder.screenPlayer.setDeviceItem(item);
                    holder.screenPlayer.setCamera(camera);
                    holder.screenPlayer.setListener(this);
                    holder.screenPlayer.setPosition(position);
                    holder.screenPlayer.updateUI();
                }
            }
        }
    }

    @Override
    public int getItemCount() {
        //计算每个RecyclerView中条目数量
        int count = itemList==null?0:itemList.size();
//        if (mIndex < mPageCount - 1) {
//            //不是最后一页，按config中pageSize显示
//            count = mPageSize;
//        } else {
//            //用总条目数量-已经显示的条目数量
//            //已经显示的条目数量=每页显示条目数量*已经显示的页数
//            count -= mPageSize * (mPageCount - 1);
//        }
        return count;
    }

    @Override
    public void onViewRecycled(@NonNull SinglePlayViewHolder holder) {
        super.onViewRecycled(holder);
        TGLog.d(TAG, "SplitScreenPlayer onViewRecycled id:" + holder.getAdapterPosition() +"  itemId: "+(mIndex * mPageSize + holder.getAdapterPosition()));
    }

    @Override
    public void onViewAttachedToWindow(@NonNull SinglePlayViewHolder holder) {
        super.onViewAttachedToWindow(holder);
        TGLog.d(TAG, "SplitScreenPlayer onViewAttachedToWindow id:" + holder.getAdapterPosition()+"  itemId: "+(mIndex * mPageSize + holder.getAdapterPosition()));
        if (holder != null) {
            holder.screenPlayer.play();
        }

    }

    @Override
    public void onViewDetachedFromWindow(@NonNull SinglePlayViewHolder holder) {
        super.onViewDetachedFromWindow(holder);
        TGLog.d(TAG, "SplitScreenPlayer onViewDetachedFromWindow  id:" + holder.getAdapterPosition()+"  itemId: "+(mIndex * mPageSize + holder.getAdapterPosition()));
        if (holder != null) {
            holder.screenPlayer.stop();
        }
    }

    @Override
    public void playerChanged( final int position) {
        TGLog.i(TAG, "SplitScreenPlayer playerChanged id:" +position);
        ((Activity)context).runOnUiThread(new Runnable() {
            @Override
            public void run() {
                notifyItemChanged(position, 1);
            }
        });


    }



    public class SinglePlayViewHolder extends RecyclerView.ViewHolder {
        public TextView tvName;
        public SplitScreenPlayer screenPlayer;
        RelativeLayout layout;

        public SinglePlayViewHolder(@NonNull View itemView) {
            super(itemView);
            layout = itemView.findViewById(R.id.rel_quarter_screen_content);
            tvName = itemView.findViewById(R.id.tv_quarter_screen_name);
            screenPlayer = itemView.findViewById(R.id.player_quarter_screen);
        }
    }
}
